#include<stdio.h>
#include<string.h>
#include "../include/test.h"
#include<stdlib.h>
int main(){
	char cmd[1000];
	char input[25][100];
	int i = 1;
       int j;
       int mount_flag=0;
       char * filesystem;
	while(1){
 
                j=0;
                if(mount_flag==1)
		  printf("[%s]#",filesystem);	
		gets(cmd);
		
		//////////tokennizing

               //strcpy(input[j],strtok(cmd," "));   // copies the command name into a[]

		char *p = strtok(cmd," ");
                
                
		while(p!=NULL)
		{


			strcpy(input[j],p);
			j++;
			p = strtok(NULL," ");



			//break;
		}



		
		////////////checking cmd
		if(strcmp(input[0], "create_vfs")==0){     // if command equals "create_vfs"
		  	
                    printf("\nCreate VFS..%s %s\n",input[1],input[2]);
                    create_file_system(input[1],atoi(input[2]));
                    
		}



		// mount_vfs command
		else if(strcmp(input[0], "mount_vfs")==0){
			 puts("...............loading..........\nvfs is mounting");
                         mount(input[1]);
                         filesystem=(char*)malloc(strlen(input[1])*sizeof(char));
         		 strcpy(filesystem,input[1]); 
                         mount_flag=1;
		}




		//makedir command
		else if(strcmp(input[0], "makedir")==0){
			//printf("%s\n",input[0] );
			//printf("%s\n",input[1] );
                        if(mount_flag)
                                
                        	create_rec_dir(input[1],input[2]);
                        else
                                printf("\nFilesystem is not mounted\n");
		}




		//create command
		else if(strcmp(input[0], "addfile")==0){
			//printf("%s\n",input[0] );
                        if(mount_flag)
                        {
                       
                         create(input[1],input[2],input[3]);
			}
			else
			{
                           printf("\nFilesystem is not mounted\n");
			}
		}


		//listfile command
		else if(strcmp(input[0], "listfile")==0){
                        if(mount_flag)
			{
				
		        	list_File(input[1],input[2]);
                        	
			}
			else
			{
				printf("\nFileSystem is not mounted\n");
			}
		}




		//unmount vfs
		else if(strcmp(input[0], "unmount_vfs")==0){
			//printf("%s\n",input[0] );
                       if(mount_flag) 
		       {	
                        unmount("demo");
			mount_flag=0;
		       }
				
		      else
                      {
			printf("\nFileSystem is not mounted\n");
                      }		
				

		}

                else if(strcmp(input[0], "listdir")==0){
			//printf("%s\n",input[0] );
		        list(input[1],atoi(input[2]),input[3]);
		}

              else if(strcmp(input[0], "updatefile")==0){
			//printf("%s\n",input[0] );
		        update_file_contents(input[1],input[2]);
		}


              else if(strcmp(input[0], "deletedir")==0){
			//printf("%s\n",input[0] );
		        removedir(input[1]);
		}

              else if(strcmp(input[0], "removefile")==0){
			//printf("%s\n",input[0] );
		        removedir(input[1]);
		}

               else if(strcmp(input[0], "movedir")==0){
			//printf("%s\n",input[0] );
		        move_dir(input[1],input[2]);
		}

               else if(strcmp(input[0], "movefile")==0){
			//printf("%s\n",input[0] );
		        move_dir(input[1],input[2]);
		}

		else if(strcmp(input[0], "exit")==0){     // exits when user enter "exit"
			 break;
		}

		else{
			printf("\nInvalid Command\n");
		}
	}


	puts("bye");
        return 0;

}
